import { Injectable } from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
import { createTransport, Transporter } from 'nodemailer';
import Mail from 'nodemailer/lib/mailer';

@Injectable()
export class EmailSerivce {
    transporter: Transporter;

    constructor(protected configService: ConfigService) {
        this.transporter = createTransport({
            host: configService.get('nodemailer_host'),
            port: configService.get('nodemailer_port'),
            secure: false,
            auth: {
                user: configService.get('nodemailer_auth_user'),
                pass: configService.get('nodemailer_auth_pass'),
            },
        });
    }

    async sendMail({ to, subject, html }: Mail.Options) {
        await this.transporter.sendMail({
            from: {
                name: '会议预约系统',
                address: '2604800581@qq.com',
            },
            to,
            subject,
            html,
        });
    }
}
